#include"ChatServer.hpp"
#include"ThreadPool.hpp"
#include"Route.hpp"
#include<iostream>
#include<memory>

void Usage(std::string proc)
{
    std::cerr << "Usage: " << proc << " localport" << std::endl;
}

// //for debug
// std::string func(const int& word,const std::string whoip,uint16_t whoport)
// {
//     return "hello";
// }

using task_t =std::function<void()>;

int main(int argc,char* argv[])
{
    if(argc!=2)
    {
        Usage(argv[0]);
        exit(0);
    }

    EnableConsoleLogStrategy();
    uint16_t port=std::stoi(argv[1]);
    
    // 1. 消息转发功能
    std::unique_ptr<Route> r = std::make_unique<Route>();

    // 2. 线程池对象
    auto tp = ThreadPool<task_t>::GetInstance();

    std::unique_ptr<ChatServer> usp=std::make_unique<ChatServer>(port,
            [&r, &tp](int sockfd, std::string message, InetAddr addr){
            task_t task = std::bind(&Route::RouteToMessage, r.get(), sockfd, message, addr);
            tp->Enqueue(task);}
        );
    usp->Init();
    usp->Start();
    return 0;
}